package com.angel.controller;

import com.angel.bean.IllCase;
import com.angel.bean.UserInfo;
import com.angel.service.IllCaseService;
import com.angel.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/pr")
public class PreAboutController {

    @Autowired
    private IllCaseService illCaseService;


    @RequestMapping("/pre")
    public String getPreAbout(){
        return "pre_about";
    }


    @RequestMapping("/preabout")
    @ResponseBody
    public Object getPerABOUT(IllCase illCase){
        Map<String,Object> map = new HashMap<>();
        map.put("success",false);
      UserInfo userInfo = UserUtils.getPrincipal().getUserInfo();
      //表示预约中
        if (illCase.getVisitDate() ==null){
            map.put("msg","预约失败,日期异常");
        }else if (illCase.getVisitDate().getTime() < (System.currentTimeMillis()-(1000*60*60*24))){
            map.put("msg","预约失败,请选择");
        }else{
            if (userInfo.getBabyId() ==null){
                map.put("msg","预约失败,请联系管理员完善宝宝信息");
            }else {
                illCase.setIllCaseState(5);
                illCase.setUserId(userInfo.getId());
                illCase.setBabyId(userInfo.getBabyId());
                int res = illCaseService.insertSelective(illCase);
                if (res !=0){
                    map.put("success",true);
                    map.put("msg","预约成功");
                }else {
                    map.put("msg","预约失败，请刷新页面重试");
                }
            }
        }
        return map;
    }
}
